package com.csw.controller;

import com.csw.entity.Dir;
import com.csw.entity.User;
import com.csw.service.DirService;
import com.csw.service.FileService;
import com.csw.util.DeleteAllByPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;

@RequestMapping("recover")
@RestController
public class RecoverController {
    @Autowired
    private FileService fileService;
    @Autowired
    private DirService dirService;

    @RequestMapping("queryAll")
    public String queryAll(HttpSession session) {

        User user = (User) session.getAttribute("user");
            if (user == null) {
                return "redirect:/user/logOut";
            }
            int status = 0;
            List<Dir> dirs = dirService.selectByStatus(status, user);
            List<com.csw.entity.File> files = fileService.selectByStatus(status, user);
            session.setAttribute("dirs", dirs);
            session.setAttribute("files", files);
            return "redirect:/jsp/front/main/detail/recover.jsp";

    }

    @RequestMapping("recorverFile")
    public String recorverFile(HttpSession session, String fileId) {

        com.csw.entity.File file = fileService.selectFileById(fileId);
            file.setStatus(1);
            fileService.updateStatus(file);
            return "redirect:/recover/queryAll";

    }

    @RequestMapping("recorverDir")
    public String recorverDir(HttpSession session, String pid) {


        Dir dir1 = dirService.selectById(pid);
            dir1.setStatus(1);
            dirService.updateStatue(dir1);
            return "redirect:/recover/queryAll";

    }

    @RequestMapping("/del")
    public String del(String fileId, HttpSession session) {

        String did = (String) session.getAttribute("did");
            Dir dir = dirService.selectById(did);
            User user = (User) session.getAttribute("user");
            com.csw.entity.File file = fileService.selectFileById(fileId);
            //完整路径
            String filepath = file.getPath() + "\\" + file.getNewName();
            File file1 = new File(filepath);
            if (file1.exists()) {
                file1.delete();
            }
            fileService.deleteFileById(fileId);
            return "redirect:/mydoc/queryOne";

    }

    /**
     * @param pid
     * @param session
     * @return
     */
    @RequestMapping("/delDir")
    public String delDir(String pid, HttpSession session) {

        User user = (User) session.getAttribute("user");
            Dir dir = dirService.selectById(pid);

            String path = dir.getPath() + "\\" + dir.getName();
            String path0 = path.replaceAll("\\\\", "\\\\\\\\\\\\\\\\");
            //找出所有的子目录///
            List<Dir> targetFolder = dirService.selectAllFolderByFatherFolder(path0, dir.getId());
            //删除根目录下的文件
            fileService.deleteFileByFolderId(dir.getId());
            //删除子目录下的文件和文件夹
            for (Dir f : targetFolder) {
                fileService.deleteFileByFolderId(f.getId());
                dirService.deleteFolderById(f.getId());
            }
            //最后删除目标文件夹
            dirService.deleteFolderById(pid);

            File fulldir = new File(path);
            //找出所有的子目录///
            //服务器执行删除实际文件夹
            DeleteAllByPath.deleteFile(fulldir);
            String did = (String) session.getAttribute("did");
            return "redirect:/mydoc/queryOne";

    }

    @RequestMapping("getMessage")
    public String getMessage(HttpSession session) {

        String message = (String) session.getAttribute("message");
            return message;

    }

    /**
     * @param session
     * @return
     */
    @RequestMapping("cleMessage")
    public String cleMessage(HttpSession session) {

        session.setAttribute("message", null);
            return "redirect:/recorver/queryAll";

    }
}
